<?php

/**
 * 
 * @author 
 * @version 
 *
 */

require_once 'Zend/Controller/Action.php';

class ManageController extends Zend_Controller_Action
{
	/**
	 * The default action - show the home page
	 */
	public function indexAction()
	{
		throw new Bcms_Exception( 'Acceso invalido' );
	}
	
	public function newAction()
	{
		$resourceId = $this->_request->getParam('rid', false);
		
		if(!$resourceId){
			throw new Bcms_Exception( 'Debes seleccionar un recurso valido.' );
		}
		
		$resource = new Bcms_Resource( $resourceId );
		
		//Zend_Debug::dump($resource->getComponents());
		$this->view->form = $resource->getForm();
	}
	
	public function editAction()
	{
		$resourceId = $this->_request->getParam('rid', false);
		$itemId = $this->_request->getParam('id', false);
		
		if( !$resourceId ) {
			throw new Bcms_Exception( 'Debes seleccionar un recurso valido.' );
		}

		if ( !$itemId ) {
			throw new Bcms_Exception( 'Debes seleccionar un item valido para editar.' );
		}
		
		$resource = new Bcms_Resource( $resourceId );
		
		//Zend_Debug::dump($resource->getComponents());
		$form = $resource->getForm();
		
		$resourceModel = $resource->getResource()->getResourceModel();
		$item = $resourceModel->fetchRow($resourceModel->getAdapter()->quoteInto( 'id = ?', $itemId ) );
		
		if ( $item != null ) {
			$form->populate( $item->toArray() );
		} else {
			throw new Bcms_Exception( 'No se ha encontrado el item seleccionado.' );
		}

		$this->view->form = $form;
	}
}